<?php

class XistsUserPeer extends BaseXistsUserPeer
{
	static public $gender = array(
	  'male' => 'Male',
	  'female' => 'Female',
	);

	static public $joinPostion = array(
	  'left' => 'Left',
	  'right' => 'Right',
	);
	
  static public function getByUsername($username)
  {
    $criteria = new Criteria();
    $criteria->add(self::USERNAME, $username);
 
    return self::doSelectOne($criteria);
  }
  
  static public function getSibling($username)
  {
    $criteria = new Criteria();
    $criteria->add(self::PLACE_UNDER_USERNAME, $username);
    $xistsUsers = self::doSelect($criteria);
    $sibling = array('left'=>false, 'right'=>false);
    foreach ($xistsUsers as $xistsUser) {
      $position = $xistsUser->getJoinPosition();
      if (array_key_exists($position, self::$joinPostion))
        $sibling[$position] = $xistsUser;
    }
    return $sibling;
  }
  
  static public function getFollowers($username, $criteria = null)
  {
    $criteria = self::getFollowerCriteria($username, $criteria);
    return self::doSelect($criteria);
  }
  
  static public function getMemberCount($username, $criteria = null)
  {
    $criteria = self::getFollowerCriteria($username, $criteria);
    return self::doCount($criteria);
  }
  
  static public function getAmountCount($username, $criteria = null)
  {
    $criteria = self::getFollowerCriteria($username, $criteria);
    $criteria->addSelectColumn('SUM(' . self::DEPOSIT_AMOUNT . ') AS amount'); 
    $stmt = self::doSelectStmt($criteria);
    return $stmt->fetchObject()->amount;
  }
  
  static public function getFollowerCriteria($username, $criteria = null)
  {
    if (!$criteria) {
      $criteria = new Criteria();
      $criteria->add(self::PLACE_UNDER_USERNAME, $username);
    } else {
      $criteria->addAnd(self::PLACE_UNDER_USERNAME, $username);
    }
    $subSelect = self::PLACE_UNDER_USERNAME . " IN ( SELECT " . self::USERNAME . " FROM xists_user WHERE place_under_username = '$username' )";
    $criteria->addOr(self::PLACE_UNDER_USERNAME, $subSelect, Criteria::CUSTOM);
    return $criteria;
  }
  
  static public function getBlankUser()
  {
    $xistsUser = new XistsUser();
    $xistsUser->setUsername('EMPTY');
    return $xistsUser;
  }
  
}